package db_test

import (
	"time"

	"github.com/nsqio/go-nsq"
)

type ConsumerT struct{}

func main() {
	InitConsumer("test", "test-channel", "127.0.0.1:4161")
	for {
		time.Sleep(time.Second * 10)
	}
}

func (c *ConsumerT) HandleMessage(message *nsq.Message) error {
	println("Got a message: ", string(message.Body))
	return nil
}

func InitConsumer(topic string, channel string, address string) {
	cfg := nsq.NewConfig()
	cfg.LookupdPollInterval = time.Second * 5
	consumer, err := nsq.NewConsumer(topic, channel, cfg)
	if err != nil {
		panic(err)
	}
	// consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
	// 	fmt.Printf("Got a message: %s\n", message.Body)
	// 	return nil
	// }))
	consumer.SetLogger(nil, 0)        // 屏蔽系统日志
	consumer.AddHandler(&ConsumerT{}) // 添加消费者接口

	if err := consumer.ConnectToNSQLookupd(address); err != nil {
		panic(err)
	}

	//建立多个nsqd连接
	// if err := c.ConnectToNSQDs([]string{"127.0.0.1:4150", "127.0.0.1:4152"}); err != nil {
	//  panic(err)
	// }

	// 建立一个nsqd连接
	// if err := c.ConnectToNSQD("127.0.0.1:4150"); err != nil {
	//  panic(err)
	// }
}
